package com.jingdianjichi.subject.handler;

import com.jingdianjichi.subject.common.enums.IsDeleteEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.convert.SubjectMultipleConverter;
import com.jingdianjichi.subject.convert.SubjectRadioConverter;
import com.jingdianjichi.subject.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.entity.SubjectInfoBO;
import com.jingdianjichi.subject.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.impl.SubjectMultipleServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目的策略类
 */
@Component
public class MultipleTypeHandler implements  SubjectTypeHandler{

    private final SubjectMultipleServiceImpl subjectMultipleService;

    public MultipleTypeHandler(SubjectMultipleServiceImpl subjectMultipleService) {
        this.subjectMultipleService = subjectMultipleService;
    }

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //多选题目的插入
        if(CollectionUtils.isEmpty(subjectInfoBO.getOptionList())) return;
        List<SubjectMultiple> subjectMultiples=new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option->{
            SubjectMultiple subjectMultiple = SubjectMultipleConverter.Instatnce.convertBOToRadio(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(IsDeleteEnum.UN_DELETED.getCode());
            subjectMultiples.add(subjectMultiple);
        });
        //插入单选题目的表
        subjectMultipleService.batchInsert(subjectMultiples);
    }

    @Override
    public SubjectOptionBO query(Long id) {
        //获取多选题
        List<SubjectMultiple> subjectMultiples= subjectMultipleService.queryBySubjectId(id);
        //将获取的信息 封装到OptionBo中
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        List<SubjectAnswerBO> subjectAnswerBOS = new ArrayList<>();
        subjectMultiples.forEach(subjectMultiple -> {
            SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
            subjectAnswerBO.setOptionType(subjectMultiple.getOptionType());
            subjectAnswerBO.setOptionContent(subjectMultiple.getOptionContent());
            subjectAnswerBO.setIsCorrect(subjectMultiple.getIsCorrect());
            subjectAnswerBOS.add(subjectAnswerBO);
        });
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
